package com.coocaa.ops.admin.v1.mergeposter.validator;

import com.coocaa.ops.plus.core.third.magic.model.BlockPositionInfo;
import com.coocaa.ops.admin.v1.mergeposter.constant.CategoryIdConstants;
import com.coocaa.ops.admin.v1.mergeposter.constant.PublishStatusConstants;
import com.coocaa.ops.admin.v1.mergeposter.model.internal.AiPosterMergeDbInfo;
import com.coocaa.ops.admin.v1.mergeposter.model.internal.BasePosterMergeInfo;
import lombok.experimental.UtilityClass;
import org.apache.commons.lang3.StringUtils;

import java.util.Objects;

/**
 * @author bjh
 * @description
 * @date 2021/1/7 10:54
 */
@UtilityClass
public class PosterMergeValidator {
    /**
     * 最小高度
     */
    private static final int MIN_HEIGHT = 231;
    /**
     * 排除的宽度和高度
     */
    private static final String[] EXCLUSION_WITH_HEIGHT = new String[]{
            "260*260"
    };

    public static boolean isCanNotDrawText(AiPosterMergeDbInfo aiPosterMergeDbInfo) {
        boolean hasBlockTitlePosition = StringUtils.isNotBlank(aiPosterMergeDbInfo.getBlockTitlePosition());
        if (hasBlockTitlePosition) {
            return true;
        }
        BlockPositionInfo blockPositionInfo = aiPosterMergeDbInfo.getBlockPositionInfo();
        if (null == blockPositionInfo) {
            return false;
        }
        boolean isHeightLess = blockPositionInfo.getHeight() <= MIN_HEIGHT;
        boolean isExcludeWidthAndHeight = isExcludeWidthAndHeight(blockPositionInfo.getWidth(), blockPositionInfo.getHeight());
        return isHeightLess || isExcludeWidthAndHeight;
    }


    public static boolean isShowTitle(BasePosterMergeInfo posterMergeInfo) {
        return Objects.equals(1, posterMergeInfo.getShowTitle())
                && StringUtils.isNotBlank(posterMergeInfo.getTitle())
                ;
    }

    public static boolean isShowSubTitle(BasePosterMergeInfo posterMergeInfo, String subTitle) {
        return Objects.equals(1, posterMergeInfo.getShowSubTitle())
                && StringUtils.isNotBlank(subTitle)
                ;
    }

    public static boolean isShowSeries(BasePosterMergeInfo posterMergeInfo) {
        return Objects.equals(1, posterMergeInfo.getShowSeries())
                && CategoryIdConstants.isSeries(posterMergeInfo.getCategoryId())
                && null != posterMergeInfo.getSeries()
                && posterMergeInfo.getSeries() > 0;
    }

    public static boolean isShowVariety(BasePosterMergeInfo posterMergeInfo) {
        return Objects.equals(1, posterMergeInfo.getShowSeries())
                && CategoryIdConstants.isVariety(posterMergeInfo.getCategoryId())
                && StringUtils.isNotBlank(posterMergeInfo.getVariety())
                && !PublishStatusConstants.isEnded(posterMergeInfo.getPublishStatus());
    }

    public static boolean isShowScore(boolean isShowSeries, BasePosterMergeInfo posterMergeInfo, boolean isShowVariety) {
        return !isShowSeries
                && !isShowVariety
                && Objects.equals(1, posterMergeInfo.getShowScore())
                && StringUtils.isNotBlank(posterMergeInfo.getScore())
                && Float.parseFloat(posterMergeInfo.getScore()) > 0
                && CategoryIdConstants.isCanShowScore(posterMergeInfo.getCategoryId());
    }

    /**
     * 是否是排除的宽高
     *
     * @param with
     * @param height
     * @return
     */
    private static boolean isExcludeWidthAndHeight(int with, int height) {
        String widthAndHeight = with + "*" + height;
        for (String wh : EXCLUSION_WITH_HEIGHT) {
            if (widthAndHeight.equals(wh)) {
                return true;
            }
        }
        return false;
    }

}
